/*
 * fuzuli : A general purpose interpreter
 * Copyright (C) 2014 Mehmet Hakan Satman <mhsatman@yahoo.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package Interpreter;

import java.util.ArrayList;

public class TryCatchExpression extends Expression {

    public TryCatchExpression(ArrayList<Expression> expr) {
        this.exprs = expr;
    }

    @Override
    public Object eval(Environment e) {
        Object result = null;
        if (exprs.size() == 2) {
            try {
                result = exprs.get(0).eval(e);
            } catch (Exception any_exp) {
                result = exprs.get(1).eval(e);
            }
        } else if (exprs.size() == 3) {
            try {
                result = exprs.get(0).eval(e);
            } catch (Exception any_exp) {
                result = exprs.get(1).eval(e);
            } finally {
                result = exprs.get(2).eval(e);
            }
        } else {
            throw new FuzuliException(null, "try-catch should be called with 2 or 3 parameters but it was " + exprs.size());
        }
        return(result);
    }
}
